package com.sergio.accounting.controller;

import com.sergio.accounting.dto.ApiResponse;
import com.sergio.accounting.repository.RecordRepository;
import com.sergio.accounting.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试控制器
 */
@Slf4j
@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
@CrossOrigin
public class TestController {

    private final UserRepository userRepository;
    private final RecordRepository recordRepository;

    /**
     * 测试数据库连接
     */
    @GetMapping("/db-connection")
    public ApiResponse<String> testDbConnection() {
        try {
            // 测试用户表
            long userCount = userRepository.count();
            log.info("用户表记录数: {}", userCount);
            
            // 测试记录表
            long recordCount = recordRepository.count();
            log.info("记录表记录数: {}", recordCount);
            
            return ApiResponse.success("数据库连接正常", 
                String.format("用户表: %d 条记录, 记录表: %d 条记录", userCount, recordCount));
        } catch (Exception e) {
            log.error("数据库连接测试失败: {}", e.getMessage(), e);
            return ApiResponse.error("数据库连接失败: " + e.getMessage());
        }
    }

    /**
     * 测试JWT用户上下文
     */
    @GetMapping("/user-context")
    public ApiResponse<String> testUserContext() {
        try {
            // 这里可以添加用户上下文测试逻辑
            return ApiResponse.success("用户上下文测试接口正常");
        } catch (Exception e) {
            log.error("用户上下文测试失败: {}", e.getMessage(), e);
            return ApiResponse.error("用户上下文测试失败: " + e.getMessage());
        }
    }
}
